//
//  ChartView.m
//  SHHCPhone
//
//  Created by gfsh on 14-6-18.
//  Copyright (c) 2014年 sheng long. All rights reserved.
//

#import "ChartView.h"

@implementation ChartView
{
    UILabel *titleLab;
    NSInteger cellWidth;
    NSInteger cellHeight;
    NSInteger xCount;
    NSInteger yCount;
    BOOL needDrawY;
}


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = GET_COLOR(@"f2f2f2");
        titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, 30)];
        titleLab.font = [UIFont systemFontOfSize:12];
        titleLab.textAlignment = NSTextAlignmentCenter;
        [self addSubview:titleLab];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame title:(NSString*)title titleColor:(NSString*)titleColor xAsis:(NSArray*)xasis yAsis:(NSArray*)yasis needDrawY:(BOOL)need
{
    self = [self initWithFrame:frame];
    if (self) {
        titleLab.text = title;
        titleLab.textColor = GET_COLOR(titleColor);
        needDrawY = need;
        if (xasis&&yasis) {
            cellWidth = (self.frame.size.width-30-10)/(xasis.count-1);
            cellHeight = (self.frame.size.height-30-30)/(yasis.count-1);
            xCount = xasis.count;
            yCount = yasis.count;
            [self drawXAsis:xasis];
            [self drawYAsis:yasis];
        }
    }
    
    return self;
}

- (void)drawXAsis:(NSArray*)xAsis
{
    CAShapeLayer *linesLayer = [CAShapeLayer layer];
    linesLayer.frame = self.bounds;
    linesLayer.fillColor = [UIColor clearColor].CGColor;
    linesLayer.backgroundColor = [UIColor clearColor].CGColor;
    linesLayer.strokeColor = GET_COLOR(@"dddddd").CGColor;
    linesLayer.lineWidth = 1;
    
    CGMutablePathRef linesPath = CGPathCreateMutable();
    
    for (NSString *str in xAsis) {
        NSInteger index = [xAsis indexOfObject:str];
        UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(30+cellWidth*index-16, self.frame.size.height-30, 40, 28)];
        lab.textAlignment = NSTextAlignmentCenter;
        lab.textColor = GET_COLOR(@"3e3e3e");
        lab.font = [UIFont systemFontOfSize:11];
        lab.text = str;
        [self addSubview:lab];
        
        CGPoint currentLinePoint = CGPointMake(34+index*(cellWidth+.5), 34);
        CGPathMoveToPoint(linesPath, NULL, currentLinePoint.x, currentLinePoint.y);
        CGPathAddLineToPoint(linesPath, NULL, currentLinePoint.x, currentLinePoint.y+self.frame.size.height-34-30);

    }
    
    linesLayer.path = linesPath;
    if (needDrawY) {
        [self.layer addSublayer:linesLayer];
    }
}

- (void)drawYAsis:(NSArray*)yAsis
{
    
    CAShapeLayer *linesLayer = [CAShapeLayer layer];
    linesLayer.frame = self.bounds;
    linesLayer.fillColor = [UIColor clearColor].CGColor;
    linesLayer.backgroundColor = [UIColor clearColor].CGColor;
    linesLayer.strokeColor = GET_COLOR(@"dddddd").CGColor;
    linesLayer.lineWidth = 1;
    
    CGMutablePathRef linesPath = CGPathCreateMutable();
    
    for (NSString *str in yAsis) {
        NSInteger index = [yAsis indexOfObject:str];
        UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 20+cellHeight*index, 30, 28)];
        lab.textAlignment = NSTextAlignmentRight;
        lab.textColor = GET_COLOR(@"3e3e3e");
        lab.font = [UIFont systemFontOfSize:11];
        lab.text = str;
        [self addSubview:lab];
        
        CGPoint currentLinePoint = CGPointMake(34, lab.frame.origin.y+lab.frame.size.height/2);
        CGPathMoveToPoint(linesPath, NULL, currentLinePoint.x, currentLinePoint.y);
        CGPathAddLineToPoint(linesPath, NULL, currentLinePoint.x + self.frame.size.width-30-10, currentLinePoint.y);
    }
    
    linesLayer.path = linesPath;
    [self.layer addSublayer:linesLayer];
}

- (void)drawCurveLines:(NSArray*)lines highlight:(NSInteger)index
{
    //line [{@"x":@"1",@"y":@"3"},{@"x":@"2",@"y":@"5"}]
    NSMutableArray *nLines = [NSMutableArray array];
    for (NSArray *line in lines) {
        if (![line isEqual:lines[index]]) {
            [nLines addObject:line];
        }
    }
    [nLines addObject:lines[index]];
    for (NSArray *line in nLines) {
        NSInteger idx = [nLines indexOfObject:line];
        NSMutableArray *points = [NSMutableArray array];
        for (NSDictionary *lPoints in line) {
            NSString *xStr = [lPoints objectForKey:@"x"];
            NSString *yStr = [lPoints objectForKey:@"y"];
            CGFloat x = [xStr floatValue]*cellWidth+34;
            CGFloat y = (yCount-[yStr floatValue])*cellHeight+30;
            CGPoint p = CGPointMake(x, y);
            [points addObject:NSStringFromCGPoint(p)];
        }
        
        UIColor *color = idx == lines.count-1?GET_COLOR(@"fe8cb6"):GET_COLOR(@"e8e8e8");
        
        CAShapeLayer *linesLayer = [CAShapeLayer layer];

        linesLayer.frame = self.bounds;
        linesLayer.fillColor = [UIColor clearColor].CGColor;
        linesLayer.backgroundColor = [UIColor clearColor].CGColor;
        linesLayer.strokeColor = color.CGColor;
        linesLayer.lineWidth = 1.5;
        CGMutablePathRef linesPath = CGPathCreateMutable();
        
        CGPoint tempP = CGPointZero;
        for (NSString *point in points) {
            CGPoint p = CGPointFromString(point);
            if (!CGPointEqualToPoint(tempP, CGPointZero)) {
                CGPathMoveToPoint(linesPath, NULL, tempP.x, tempP.y);
                CGPathAddLineToPoint(linesPath, NULL, p.x, p.y);
            }
            tempP = p;
        }
        
        linesLayer.path = linesPath;
        
        [self.layer addSublayer:linesLayer];
        
        
        CAShapeLayer *circlesLayer = [CAShapeLayer layer];
        circlesLayer.frame = self.bounds;
        circlesLayer.fillColor = [UIColor clearColor].CGColor;
        circlesLayer.backgroundColor = [UIColor clearColor].CGColor;
        circlesLayer.strokeColor = color.CGColor;
        circlesLayer.lineWidth = 3;
        
        CGMutablePathRef circlesPath = CGPathCreateMutable();
        
        for (NSString *point in points) {
            CGPoint p = CGPointFromString(point);
            CGPathAddEllipseInRect(circlesPath, NULL, CGRectMake(p.x - 1.5, p.y - 1.5, 3, 3));
        }
        
        circlesLayer.path = circlesPath;
        [self.layer addSublayer:circlesLayer];
    }
}


@end
